﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Security.Cryptography;

namespace CalendarScreenSaver
{
    /// <summary>
    /// Interaction logic for SettingsWindow.xaml
    /// </summary>
    public partial class SettingsWindow : Window
    {
        public SettingsWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string inputUserName = txtUsername.Text.Trim().ToLower();
            string inputCalendarName = txtCalendarName.Text.Trim().ToLower();

            if (_settings != null)
            {
                if (!_settings.UserName.ToLower().Equals(inputUserName) || !_settings.CalendarName.ToLower().Equals(inputCalendarName))
                {
                    _settings.UserName = inputUserName;
                    _settings.CalendarName = inputCalendarName;
                    SettingsManager.SaveSettings(_settings);
                }
            }
            else
            {
                _settings = new Settings { UserName = inputUserName, CalendarName = inputCalendarName };
                SettingsManager.SaveSettings(_settings);
            }

            MessageBox.Show("Your settings have been successfully saved.", "Alert", MessageBoxButton.OK, MessageBoxImage.Information);
            Close();
        }

        Settings _settings = null;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _settings = SettingsManager.ReadSettings();
            if (_settings != null)
            {
                txtUsername.Text = _settings.UserName;
                txtCalendarName.Text = _settings.CalendarName;
            }
        }
    }
}
